package com.globant.bugfixer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.globant.bugfixer.Report;

import org.springframework.jdbc.core.RowMapper;


public class TrafficProductReplace extends GenericProductReplace  {

	@Override
	public void deleteReport(Report presentReport) {
		String query = "DELETE FROM akn_traffic_fact WHERE product_dim_id=? AND language_dim_id=? AND country_dim_id=? and channel_dim_id=? and time_dim_id=?";
		this.getJdbcTemplate().update(query, new Object[] { presentReport.getProduct_id(),
															presentReport.getLanguage_id(),presentReport.getCountry_id(),
															presentReport.getChannel_id(),presentReport.getTime_id()});			
	}

	@Override
	public void updateReport(int traffic, Report presentReport) {
		String query = "UPDATE akn_traffic_fact SET traffic = ? WHERE product_dim_id=? AND language_dim_id=? AND country_dim_id=? and channel_dim_id=? and time_dim_id=?";
		this.getJdbcTemplate().update(query, new Object[] {traffic,presentReport.getProduct_id(),
															presentReport.getLanguage_id(),presentReport.getCountry_id(),
															presentReport.getChannel_id(),presentReport.getTime_id()});		
	}

	@Override
	public Report isCombinationPresent(Report report_to_replace,
			ArrayList<Report> collection_reports) {
		Report resultReport=null;
		for(int i=0;i<collection_reports.size();i++){
			Report new_report=collection_reports.get(i);
			if(	   new_report.getLanguage_id()==report_to_replace.getLanguage_id()
				&& new_report.getTime_id()==report_to_replace.getTime_id()
				&& new_report.getChannel_id()==report_to_replace.getChannel_id()
				&& new_report.getCountry_id()==report_to_replace.getCountry_id()
				&& new_report.getProduct_id()!=report_to_replace.getProduct_id()){
				
				resultReport=new_report;
			}
		}
		return resultReport;
		
	}

	@Override
	public void insertReport(Report report_to_replace, int product_id) {
		String query =	"INSERT INTO akn_traffic_fact (time_dim_id , channel_dim_id, product_dim_id, language_dim_id, country_dim_id, traffic)" +
						" VALUES (?, ?, ?, ?, ?, ?)";		
		this.getJdbcTemplate().update(query, new Object[] {report_to_replace.getTime_id(), report_to_replace.getChannel_id(),
															product_id, report_to_replace.getLanguage_id(),
															report_to_replace.getCountry_id(),report_to_replace.getTraffic()});		
	}

	@Override
	public ArrayList<Report> getElementsFromReport(int product_id) {
		String query = "SELECT * FROM akn_traffic_fact WHERE product_dim_id = ?";
		List<?> resultSet = this.getJdbcTemplate().
				query(query, new Object[]{product_id}, new TrafficReportRowMapper()); 		
		ArrayList<Report> reports = new ArrayList<Report>();
		
		for (Object item : resultSet){
			reports.add((Report) item);
		}
		 		
		return (reports);
	}
	@Override
	public ArrayList<Report> getReport(Report report) {
		String query = "SELECT * FROM akn_traffic_fact WHERE product_dim_id = ? AND time_dim_id=? AND channel_dim_id=? AND language_dim_id=?";
		List<?> resultSet = this.getJdbcTemplate().
				query(query, new Object[]{report.getProduct_id(),report.getTime_id(),report.getChannel_id(),report.getLanguage_id()}, new TrafficReportRowMapper()); 		
		ArrayList<Report> reports = new ArrayList<Report>();
		
		for (Object item : resultSet){
			reports.add((Report) item);
		}
		 		
		return (reports);
	}
	public class TrafficReportRowMapper implements RowMapper {

		@Override
		public Report mapRow(ResultSet rs, int rowNum) throws SQLException {
			Report report = new Report();
			
			report.setTime_id(Integer.parseInt(rs.getString("time_dim_id")));
			report.setChannel_id(Integer.parseInt(rs.getString("channel_dim_id")));
			report.setLanguage_id(Integer.parseInt(rs.getString("language_dim_id")));
			report.setProduct_id(Integer.parseInt(rs.getString("product_dim_id")));
			report.setCountry_id(Integer.parseInt(rs.getString("country_dim_id")));
			report.setTraffic(Integer.parseInt(rs.getString("traffic")));

			
			return report;
		}

	}
	@Override
	public Report isCombinationPresent(Report report_to_replace, int product_id) {
		System.out.println("METHOD INPUT"+report_to_replace.toString());
		String query = "SELECT * FROM akn_traffic_fact WHERE product_dim_id = ? AND time_dim_id=? AND channel_dim_id=? AND language_dim_id=?";
		List<?> resultSet = this.getJdbcTemplate().
				query(query, new Object[]{product_id,report_to_replace.getTime_id(),report_to_replace.getChannel_id(),report_to_replace.getLanguage_id()}, new TrafficReportRowMapper()); 		
		ArrayList<Report> reports = new ArrayList<Report>();
		System.out.println("LIST:"+resultSet.size());
		for (Object item : resultSet){
			reports.add((Report) item);
		}
		if(!reports.isEmpty()){			
			return reports.get(0);	
		}
		return null;
	}
}
